package cn.com.ourclass.webviewnew.wb.config;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hc.framework.utils.CheckNetwork;

import cn.com.ourclass.webviewnew.wb.WebViewActivity;


/**
 * Created by Admin on 2017/3/1.
 * 监听网页链接:
 * - 优酷视频直接跳到自带浏览器
 * - 根据标识:打电话、发短信、发邮件
 * - 进度条的显示
 * - 添加javascript监听
 */
public class MyWebViewClient extends WebViewClient{
    private IWebPageView mIWebPageView;
    private WebViewActivity mActivity;


    public MyWebViewClient(IWebPageView mIWebPageView) {
        this.mIWebPageView = mIWebPageView;
        mActivity = (WebViewActivity) mIWebPageView;

    }

    /**
     * 检测网页加载的互联网资源，防止电信运营商
     * @param view
     * @param url
     * @return
     */
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        /**
         * 用于屏蔽电信运营商广告
         * 把含端口好的直接屏蔽掉
         */
        /*
        String whiteKey = mActivity.getResources().getString(R.string.web_res_white_key);
        String[] whiteKeys = whiteKey.split("#");
        for(int i = 0; i < whiteKeys.length ; i++){
            if(url.contains(whiteKeys[i])){
                Log.e("TAG",whiteKeys[i]);
                return null;
            }
        }
        */
        //正则判断字符串中是否包含端口号，不是很精准，效率高
        Log.e("tttt", "shouldInterceptRequest: "+url );
        int ifchangyan=url.indexOf("changyan.sohu.com");
        if(ifchangyan>=0){
        }else{
            if(url.matches(".*:[1-9]+.*")){
                return new WebResourceResponse(null, null, null);
            }
        }

        return null;

    }


    @SuppressWarnings("deprecation")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // 优酷视频跳转浏览器播放
        /*
        if (url.startsWith("http://v.youku.com/")) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addCategory("android.intent.category.BROWSABLE");
            Uri content_url = Uri.parse(url);
            intent.setData(content_url);
            mActivity.startActivity(intent);
            return true;

            // 电话、短信、邮箱
        } else */
        //监听关闭
        if(url.startsWith("action:close")){
            mActivity.finish();
            return true;
        }
        mActivity.mUrl = url;
        //Log.e("tttt", "shouldOverrideUrlLoading: "+url);

        //跳本地页面
        int a=url.indexOf("actionopen:");
        if(a>=0){
            String[] sourceStrArray = url.split("actionopen:");
            String newurl="file:///android_asset/html"+sourceStrArray[1];

            boolean hastitle = sourceStrArray[1].contains("title=false");

            if(hastitle){
                WebViewActivity.loadUrl(mActivity, newurl,true,"");
            }else{
                WebViewActivity.loadUrl(mActivity, newurl,true,"");
            }

            return true;
        }

        //跳本地页面
        int r=url.indexOf("actionreload:");
        if(r>=0){
            String[] sourceStrArray = url.split("actionreload:");

            String newurl="file:///android_asset/html"+sourceStrArray[1];

            boolean hastitle = sourceStrArray[1].contains("title=false");

            if(hastitle){
                WebViewActivity.restUrl(mActivity, newurl, true,"");
            }else{
                WebViewActivity.restUrl(mActivity, newurl, true,"");
            }

            return true;
        }

        //直接跳线上页面
        int b=url.indexOf("actiongo:");
        if(b>=0){

            String[] sourceStrArray2 = url.split("actiongo:");
            String newurl=sourceStrArray2[1];
            WebViewActivity.loadUrl(mActivity, newurl,true,"");

            //WebActivity.loadUrl(mActivity, newurl,"");
            return true;
        }
        //跳本地页面
        int c=url.indexOf("actionto:");
        if(c>=0){
            String[] sourceStrArray3 = url.split("actionto:");

            switch (sourceStrArray3[1]){
                case "homepage":
                    //WebViewActivity.gohome(mActivity);
                    break;
            }
            return true;
        }

        if (url.startsWith(WebView.SCHEME_TEL) || url.startsWith("sms:") || url.startsWith(WebView.SCHEME_MAILTO)) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                mActivity.startActivity(intent);
            } catch (ActivityNotFoundException ignored) {
            }
            return true;
        }

        return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // html加载完成之后，添加监听图片的点击js函数
        mIWebPageView.addImageClickListener();
        super.onPageFinished(view, url);
    }

    // 视频全屏播放按返回页面被放大的问题
    @Override
    public void onScaleChanged(WebView view, float oldScale, float newScale) {
        super.onScaleChanged(view, oldScale, newScale);
        if (newScale - oldScale > 7) {
            view.setInitialScale((int) (oldScale / newScale * 100)); //异常放大，缩回去。
        }
    }
}